import { trim, ajax } from '../../helper/helper'
import upload from '../static/upload'
import { message } from 'antd'

message.config({
    top: 150,
    duration: 3,
});
const changeAvatar = (dispatch, url, parentCallback) => {
    dispatch({
        type: 'change-loading-status'
    });
    ajax({
        url: '/upload/userAvatar',
        type: 'POST',
        data: { avatar: url },
        dataType: 'json',
        success: data => {
            dispatch({
                type: 'change-loading-status'
            });
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success)
                parentCallback({ success: true, message: data.result.message });
            else
                parentCallback({ success: false, message: data.error });

        }

    });

}

const changeUserName = (dispatch, event) => {
    dispatch({
        type: 'set-username',
        val: event.target.value
    })
}

const changeNickName = (dispatch, event) => {
    dispatch({
        type: 'set-nickname',
        val: event.target.value
    })
}

const changeEmail = (dispatch, event) => {
    dispatch({
        type: 'set-email',
        val: event.target.value
    })
}

const submitProfile = (dispatch, data) => {
    const hasError = validate(data, dispatch).filter((i) => i.type == 'profile').length;

    if (!hasError) {

        ajax({
            type: 'post',
            url: '/settings/profile',
            data: data,
            success: (res) => {
                try {
                    res = JSON.parse(res);
                } catch (error) {

                }
                if (res.success) {
                    message.success('修改个人信息成功');
                } else {
                    message.error('修改失败，请稍后重试');
                }
            }
        })
    }
}

const changeOldPassword = (dispatch, event) => {
    dispatch({
        type: 'set-oldPassword',
        val: event.target.value
    })
}

const changeNewPassword = (dispatch, event) => {
    dispatch({
        type: 'set-newPassword',
        val: event.target.value
    })
}

const changeNewPasswordRepeat = (dispatch, event) => {
    dispatch({
        type: 'set-newPasswordRepeat',
        val: event.target.value
    })
}

const submitPassword = (dispatch, data) => {
    const hasError = validate(data, dispatch).filter((i) => i.type == 'password').length;

    if (!hasError) {
        delete data.newPasswordRepeat;
        ajax({
            type: 'post',
            url: '/settings/profile',
            data: data,
            success: (res) => {
                res = JSON.parse(res);
                if (!res.success) {
                    message.error('原密码错误');
                }
                else {
                    message.success('密码更新成功');
                    dispatch({
                        type: 'set-oldPassword',
                        val: ''
                    })
                    dispatch({
                        type: 'set-newPassword',
                        val: ''
                    })
                    dispatch({
                        type: 'set-newPasswordRepeat',
                        val: ''
                    })

                }
            }
        })
    }
}

const validate = (data, dispatch) => {
    let errors = [];
    if ('userName' in data && !trim(data.userName)) {
        errors.push({
            id: 'empty-username',
            type: 'profile',
            target: 'userName',
            error: '用户名不能为空'
        })
    }

    if ('nickName' in data && !trim(data.nickName)) {
        errors.push({
            id: 'empty-nickname',
            type: 'profile',
            target: 'nickName',
            error: '昵称不能为空'
        })
    }

    if ('email' in data && !trim(data.email)) {
        errors.push({
            id: 'empty-email',
            type: 'profile',
            target: 'email',
            error: '邮箱不能为空'
        })
    }

    if ('oldPassword' in data && !trim(data.oldPassword)) {
        errors.push({
            id: 'empty-oldPassword',
            type: 'password',
            target: 'oldPassword',
            error: '旧密码不能为空'
        })
    }

    if ('newPassword' in data && (trim(data.newPassword).length < 6)) {
        errors.push({
            id: 'too-short-newPassword',
            type: 'password',
            target: 'newPassword',
            error: '新密码至少6位'
        })
    }

    if ('oldPassword' in data && 'newPassword' in data && trim(data.newPassword) && trim(data.oldPassword) == trim(data.newPassword)) {
        errors.push({
            id: 'repetition-newPassword',
            type: 'password',
            target: 'newPassword',
            error: '新旧密码不能相同'
        })
    }

    if ('newPasswordRepeat' in data && !trim(data.newPasswordRepeat)) {
        errors.push({
            id: 'empty-newPasswordRepeat',
            type: 'password',
            target: 'newPasswordRepeat',
            error: '确认密码不能为空'
        })
    }

    if ('newPassword' in data && 'newPasswordRepeat' in data && trim(data.newPassword) && trim(data.newPassword) != trim(data.newPasswordRepeat)) {
        errors.push({
            id: 'difference-newPasswordRepeat',
            type: 'password',
            target: 'newPasswordRepeat',
            error: '两次输入的密码不一致'
        })
    }

    dispatch && dispatch({
        type: 'set-errors',
        val: errors
    });

    return errors
}

//table
const paymentListRenderData = function (dispatch, params) {
    ajax({
        url: '/business/getBillingHistory',
        data: params,
        success: (data) => {
            try { data = JSON.parse(data) } catch (err) { };
            if (data && data.success) {
                dispatch({
                    type: 'payment-list-render-data',
                    val: {
                        data: data.data,
                        count: 0
                    }
                })
            } else {
                dispatch({
                    type: 'payment-list-render-data',
                    val: {
                        data: [],
                        count: 0
                    }
                })
            }
        },
        error: () => dispatch({
            type: 'payment-list-render-data',
            val: {
                data: [],
                count: 0
            }
        })
    })
}

const paymentListRequest = function (dispatch, params) {
    dispatch({
        type: 'payment-list-request',
        val: params
    })
}

export const actions = {
    changeAvatar: changeAvatar,
    changeUserName: changeUserName,
    changeNickName: changeNickName,
    changeEmail: changeEmail,
    submitProfile: submitProfile,
    changeOldPassword: changeOldPassword,
    changeNewPassword: changeNewPassword,
    changeNewPasswordRepeat: changeNewPasswordRepeat,
    submitPassword: submitPassword,
    validate: validate,
    paymentListRenderData: paymentListRenderData,
    paymentListRequest: paymentListRequest
}